// 订单相关类型定义
import { OrderType } from './instrument';
import { TimeRange, Address } from './common';
import { UserInfo } from './user';

// 订单状态枚举
export enum OrderStatus {
  PENDING_REVIEW = 'pending_review', // 待审核
  PENDING_CONFIRMATION = 'pending_confirmation', // 待确认
  WAITING_EXPERIMENT = 'waiting_experiment', // 待实验
  IN_PROGRESS = 'in_progress', // 实验中
  PENDING_SETTLEMENT = 'pending_settlement', // 待结算
  COMPLETED = 'completed', // 已完成
  TRAINING_PASSED = 'training_passed', // 完成（培训通过）
  TRAINING_FAILED = 'training_failed', // 完成（培训不通过）
  REJECTED = 'rejected', // 已驳回
  CLOSED = 'closed', // 已关闭
  CANCELLED = 'cancelled', // 已取消
  REFUNDED = 'refunded', // 已退款
}

// 支付方式枚举
export enum PaymentMethod {
  PERSONAL_BALANCE = 'personal_balance', // 个人余额
  TEAM_BALANCE = 'team_balance', // 团队余额
  TEAM_CREDIT = 'team_credit', // 团队额度
}

// 实验类型枚举
export enum ExperimentType {
  NORMAL = 'normal', // 正常实验
  TRAINING = 'training', // 培训
  COURSE = 'course', // 试课
  SIMPLE = 'simple', // 简求订单
}

// 样品邮寄方式枚举
export enum SampleShippingMethod {
  SELF_DELIVERY = 'self_delivery', // 自带样品
  MAIL_NO_RETURN = 'mail_no_return', // 样品邮寄-不需寄回
  MAIL_WITH_RETURN = 'mail_with_return', // 样品邮寄-需寄回
}

// 快递公司枚举
export enum ExpressCompany {
  SF = 'sf', // 顺丰
  YTO = 'yto', // 圆通
  ZTO = 'zto', // 中通
  STO = 'sto', // 申通
  EMS = 'ems', // EMS
  JD = 'jd', // 京东
}

// 订单基本信息
export interface OrderInfo {
  id: string;
  orderNumber: string;
  experimentName: string;
  experimentType: ExperimentType;
  experimentLevel: string;
  instrumentName: string;
  sampleInfo: string;
  orderAmount: number;
  reservationTime: string;
  createdAt: string;
  createdBy: {
    id: string;
    name: string;
    phone: string;
  };
  responsiblePerson: {
    id: string;
    name: string;
    phone: string;
  };
  orderStatus: string;
  status: OrderStatus;
  orderType?: string; // 订单类型：sample_delivery, engineer_operation, self_operation
}

// 需求订单（多仪器）
export interface DemandOrder {
  id: string;
  orderNumber: string;
  userId: string;
  user: UserInfo;
  title: string; // 需求名称
  description: string; // 需求描述
  status: OrderStatus;
  totalAmount: number;
  paymentMethod: PaymentMethod; // 支付方式
  
  // 关联的仪器订单
  instrumentOrders: DemandInstrumentOrder[];
  
  createdAt: string;
  updatedAt: string;
}

// 需求订单中的仪器项
export interface DemandInstrumentOrder {
  id: string;
  instrumentId: string;
  instrumentName: string;
  instrumentCategory: string;
  amount: number;
  status: OrderStatus;
  responsiblePerson: {
    id: string;
    name: string;
    phone: string;
  };
  // 每个仪器可以有独立的状态和负责人
}

// 样品邮寄信息
export interface SampleShipping {
  // 样品寄出
  outbound?: {
    expressCompany: ExpressCompany;
    trackingNumber: string;
    shippedAt: string;
  };
  
  // 样品寄回
  inbound?: {
    expressCompany: ExpressCompany;
    trackingNumber: string;
    shippedAt: string;
  };
}

// 上下机记录
export interface MachineTimeRecord {
  id: string;
  orderId: string;
  type: 'on' | 'off';
  timestamp: string;
  operatorId: string;
  operatorName: string;
}

// 订单操作记录
export interface OrderOperationRecord {
  id: string;
  orderId: string;
  operatorId: string;
  operatorName: string;
  action: string;
  fromStatus?: OrderStatus;
  toStatus?: OrderStatus;
  remark?: string;
  createdAt: string;
}

// 订单搜索参数
export interface OrderSearchParams {
  keyword?: string; // 订单号或用户信息
  instrumentId?: string;
  orderType?: OrderType;
  status?: OrderStatus;
  paymentMethod?: PaymentMethod;
  createdAt?: [string, string];
  current?: number;
  pageSize?: number;
}

// 需求订单搜索参数
export interface DemandOrderSearchParams {
  keyword?: string; // 订单号、需求名称或用户信息
  status?: OrderStatus;
  paymentMethod?: PaymentMethod;
  instrumentCategory?: string;
  createdAt?: [string, string];
  current?: number;
  pageSize?: number;
}

// 订单表单
export interface OrderForm {
  instrumentId: string;
  orderType: OrderType;
  experimentType: ExperimentType;
  reservationTime: TimeRange[];
  sampleInfo: string;
  sampleShippingMethod: SampleShippingMethod;
  sampleAddress?: Address;
  selectedConsumables: {
    consumableId: string;
    quantity: number;
  }[];
  selectedServiceIds: string[];
  paymentMethod: PaymentMethod;
  teamId?: string;
  applicationFormData?: Record<string, unknown>;
}

// 需求订单表单
export interface DemandOrderForm {
  title: string;
  description: string;
  paymentMethod: PaymentMethod;
  teamId?: string;
  instrumentOrders: {
    instrumentId: string;
    notes?: string;
  }[];
}

// 修改订单金额表单
export interface ModifyOrderAmountForm {
  orderId: string;
  newAmount: number;
  remark?: string;
}

// 上传数据表单
export interface UploadDataForm {
  orderId: string;
  files: File[];
  description?: string;
}

// 订单操作请求
export interface OrderActionRequest {
  orderId: string;
  action: string;
  remark?: string;
  data?: Record<string, unknown>;
}

// 需求订单操作请求
export interface DemandOrderActionRequest {
  demandOrderId: string;
  instrumentOrderId?: string; // 针对特定仪器的操作
  action: string;
  remark?: string;
  data?: Record<string, unknown>;
}

// 批量操作请求
export interface BatchOrderActionRequest {
  orderIds: string[];
  action: string;
  remark?: string;
}

// 订单统计信息
export interface OrderStatistics {
  total: number;
  pendingReview: number;
  pendingConfirmation: number;
  waitingExperiment: number;
  inProgress: number;
  pendingSettlement: number;
  completed: number;
  rejected: number;
  closed: number;
  cancelled: number;
  refunded: number;
}

// 需求订单详细信息（包含完整的关联数据）
export interface DemandOrderDetail extends DemandOrder {
  // 完整的用户信息
  user: UserInfo;
  
  // 完整的仪器订单信息（包含仪器详情）
  instrumentOrders: (DemandInstrumentOrder & {
    instrument: {
      id: string;
      name: string;
      category: string;
      subcategory: string;
      price: number;
      unit: string;
    };
    operationRecords: OrderOperationRecord[];
    machineTimeRecords: MachineTimeRecord[];
  })[];
  
  // 支付信息
  paymentInfo?: {
    method: PaymentMethod;
    teamId?: string;
    teamName?: string;
    paidAt?: string;
    refundedAt?: string;
  };
  
  // 操作记录
  operationRecords: OrderOperationRecord[];
} 